package com.ssm.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.sfzy.entity.Article;
import com.ssm.sfzy.entity.Teacher;
import com.ssm.sfzy.mapper.ArticleMapper;
import com.ssm.sfzy.mapper.TeacherMapper;
import com.ssm.sfzy.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private TeacherMapper teacherMapper;
    @Override
    public void add(Article entity) {
        articleMapper.insert(entity);
    }

    @Override
    public void edit(Article entity) {
        articleMapper.update(entity);
    }

    @Override
    public void remove(Integer id) {
        articleMapper.delete(id);
    }

    @Override
    public void removeBatch(Integer[] ids) {
        articleMapper.deleteBatch(ids);
    }

    @Override
    public List<Article> find(Article entity) {
        return articleMapper.select(entity);
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, Article entity) {
        //
        PageHelper.startPage(pageNum, pageSize);
        List<Article> list = articleMapper.select(entity);
        for (Article article : list){
            Integer teacherId = article.getTeacherId();
            Teacher teacher = teacherMapper.selectById(teacherId);
            String name = teacher.getName();
            article.setTeacherName(name);
        }

        PageInfo<Article> pageInfo = new PageInfo<>(list);
        // 整合处理
        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return map;
    }

    @Override
    public Article findById(Integer id) {
        return articleMapper.selectById(id);
    }
}
